package com.foreveross.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.LogDAO;
import com.foreveross.dao.ProductTypeDAO;
import com.foreveross.vo.Log;
import com.foreveross.vo.ProductType;

@Service("productTypeService")
@Transactional
public class ProductTypeService {

	@Resource
	public ProductTypeDAO productTypeDAO;

	public long getProductTypeListCount(String proCode, String pro_name,
			String pro_line_name) throws Exception {
		return productTypeDAO.findProductTypeListCount(proCode, pro_name,
				pro_line_name);
	}

	public List<ProductType> findProductTypeList(String proCode,
			String pro_name, String pro_line_name,
			String pageIndex, String pageSize) throws Exception {
		List<ProductType> list = productTypeDAO.findProductTypeList(proCode,
				pro_name, pro_line_name, pageIndex, pageSize);

		return list;
	}
	
	
	public List<ProductType> findProductTypeSelect(String proCode) throws Exception{
		List<Object[]> list = productTypeDAO.findProductTypeSelect();
		List<ProductType> productTypeList = new ArrayList<ProductType>();
		for(Object[] o :list){
			ProductType productType = new ProductType();
			productType.setId((String)o[0]);
			productType.setProCode((String)o[1]);
			productType.setProName((String)o[2]);
			if(productType.getProCode().equals(proCode))
				productType.setSelected(true);
			productTypeList.add(productType);
		}
		return productTypeList;
	}
	
	
	public List<ProductType> findProductTypeLineSelect(String proCode,String proLineCode) throws Exception {
		List<Object[]> list = productTypeDAO.findProductTypeLineSelect(proCode);
		List<ProductType> productTypeList = new ArrayList<ProductType>();
		for(Object[] o :list){
			ProductType productType = new ProductType();
			productType.setId((String)o[0]);
			productType.setProLineCode((String)o[1]);
			productType.setProLineName((String)o[2]);
			if(productType.getProLineCode().equals(proLineCode))
				productType.setSelected(true);
			productTypeList.add(productType);
		}
		return productTypeList;
	}


	@Transactional
	public void deleteProductTypeList(String productTypeIds) throws Exception {
		for (String productTypeId : productTypeIds.split(",")) {
			productTypeDAO.deleteProductType(productTypeId);
		}
	}

	@Transactional
	public <T> void saveOrUpdate(T entity, String type) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			productTypeDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			productTypeDAO.updateObject(entity);
		}

	}


}
